App.require(["yDate"], function (yDate) {
    var app = App.getCurrent();

    app.definePopUp("strdatePicker", {
        sureHandler: function (ev) {
            var minDate = this.data.minDate, maxDate = this.data.maxDate, yDate = this.data.yDate;
            if (maxDate && maxDate.compare(yDate.year, yDate.month, yDate.day) === -1) {
                yDate.year = maxDate.year;
                yDate.month = maxDate.month;
                yDate.day = maxDate.day;
            }
            if (minDate && minDate.compare(yDate.year, yDate.month, yDate.day) === 1) {
                yDate.year = minDate.year;
                yDate.month = minDate.month;
                yDate.day = minDate.day;
            }
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datepickersure", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            else {
                app.currentPage.dispatchEvent({ type: "datepickersure", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            this.hidden();
        },
        cancelHandler: function (ev) {
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datepickercancel", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            else {
                app.currentPage.dispatchEvent({ type: "datepickercancel", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            this.hidden();
        },
        clearHandler: function (ev) {
            if (this.data.target) {
                this.data.target.dispatchEvent({ type: "datepickerclear", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            else {
                app.currentPage.dispatchEvent({ type: "datepickerclear", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            this.hidden();
        },
        beforeShow: function () {
            if (document.activeElement) document.activeElement.blur();
            var data = this.data;
            if (!(data.year || data.month || data.day)) {
                var now = new Date();
                data.year = now.getFullYear();
                data.month = now.getMonth() + 1;
                data.day = now.getDate();
            }
            data.title = data.title || "选择时间";
            data.yDate = new yDate(data.year, data.month, data.day);
            if (data.min) {
                data.minDate = new yDate(data.min.year, data.min.month, data.min.day);
            }
            if (data.max) {
                data.maxDate = new yDate(data.max.year, data.max.month, data.max.day);
            }
            this.attachDiyEvent("sure", this.sureHandler);
            this.attachDiyEvent("cancel", this.cancelHandler);
            this.attachDiyEvent("clear", this.clearHandler);
            return true;
        },
    })
});